<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Phone Service class for communication between controllers and persistent storage
 */
class Core_Service_Phone extends RedCross_Service_ServiceAbstract
{
    /**
     * Constructor
     * @param Core_Gateway_Phone $gateway
     */
    public function __construct(Core_Gateway_Phone $gateway)
    {
        parent::__construct($gateway, 'Core_Model_Phone');
    }

    /**
     * Deletes a phone object from the database
     * Checks if the member that has the phone number has more than
     * just the one phone number to be deleted. If not, aborts the deletion
     * @param Core_Model_Phone The phone model to be deleted
     * @return bool Whether or not the deletion was successful
     */
    public function delete($phoneModel)
    {
        $this->checkAcl('delete');

        if(!$phoneModel instanceof Core_Model_Phone) {
            throw new RedCross_Service_Exception('Incorrect parameter given');
        }

    	$member = $phoneModel->Member;
    	if(count($member->Phonenumbers) > 1) {
            return $phoneModel->delete();
    	} else {
    		return false;
    	}
    }
}